package org.chen;

import org.chen.pool.JedisConnect;

public class SetJedisTest {
  public static void main(String[] args) {
    JedisConnect connect = new JedisConnect();
    connect.execute(jedis -> {
      jedis.flushDB();

      //存,其实命令有点类似list,只不过set是无序的
      jedis.sadd("a1","a","b","c");
      //返回key中所有的member
      System.out.println(jedis.smembers("a1"));//[a, b, c]

      //移除key中指定的成员
      jedis.srem("a1","a");
      System.out.println(jedis.smembers("a1"));//[b, c]

      //key中是否存在member，1：存在，0：不存在
      System.out.println(jedis.sismember("a1", "a"));//false
      System.out.println(jedis.sismember("a1", "b"));//true

      //返回key的member数量
      System.out.println(jedis.scard("a1"));//2

      jedis.sadd("a1","a","d","e","f","g","h");

      //随机返回key中的count个member,不传count就代表一个
      System.out.println(jedis.srandmember("a1"));
      System.out.println(jedis.srandmember("a1",2));
      System.out.println(jedis.srandmember("a1",3));

      //随机弹出（移除）一个member
      System.out.println(jedis.spop("a1"));

      jedis.sadd("a2","1","2","3");
      //将key中的一个member移到另一个key中
      jedis.smove("a1","a2","a");
      System.out.println(jedis.smembers("a1"));//[d, h, g, f, e, b]
      System.out.println(jedis.smembers("a2"));//[a, 2, 1, 3]

      jedis.sadd("a2","e","c");

      //返回一个集合与另N个集合的差集
      System.out.println(jedis.sdiff("a1", "a2"));//[h, b, f, g]
      System.out.println(jedis.sdiff("a2", "a1"));//[a, 1, 2, 3]

      //返回多个集合的交集
      System.out.println(jedis.sinter("a1", "a2"));//[e]

      //将一个集合与另N个集合的差集存为一个新集合
      jedis.sdiffstore("a3","a1","a2");
      System.out.println(jedis.smembers("a3"));//[d, f, g, h, b]

      //将多个集合的交集存为一个新集合
      jedis.sinterstore("a4","a1","a3");
      System.out.println(jedis.smembers("a4"));//[d, h, g, f, b]

      //多个集合的并集
      System.out.println(jedis.sunion("a1", "a2"));//[2, e, b, c, h, 3, a, 1, d, f]

      //将多个集合的并集存到新集合中
      jedis.sunionstore("a5","a1","a2");
      System.out.println(jedis.smembers("a5"));//[2, e, b, c, g, 3, a, 1, d, f]

    });
  }
}
